package com.yboot.service.boot.test.holiday;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.yboot.starter.common.core.userdetails.LoginUser;
import com.yboot.starter.common.core.web.vo.Result;
import com.yboot.stater.websocket.core.api.SimpMessagingEnhanceTemplate;
import lombok.RequiredArgsConstructor;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 登录
 *
 * @author yangcs
 * @date 2025/8/19
 */
@RequiredArgsConstructor
@RestController
public class LoginController {

    private final SimpMessagingEnhanceTemplate simpMessagingEnhanceTemplate;
    private final String TOPIC = "/topic/login";

    /**
     * 登录测试
    @GetMapping("/login")
    public Result shiroLogin(String username, String password, HttpServletRequest request) {
        //Shiro实现登录 @TODO 自己校验登录
        Subject subject = SecurityUtils.getSubject();
        String token = JwtUtil.sign(username, password);
        //如果获取不到用户名就是登录失败，但登录失败的话，会直接抛出异常
        TokenUtils.setToken(username,
                StrUtil.blankToDefault(TokenUtils.getToken(username), token));
        return Result.ok(TokenUtils.getToken(username));
    }
     @SaIgnore
     @GetMapping("/login")
     public Result saTokenLogin(String username, String password, HttpServletRequest request) {
         LoginUser loginUser = new LoginUser();
         loginUser.setUsername("admin");
         loginUser.setPassword("123456");
         // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
         if(loginUser.getUsername().equals(username) && loginUser.getPassword().equals(password)) {
             StpUtil.login(10001, "PC");
             StpUtil.getSession().set(SaSession.USER, loginUser);
             String token = StpUtil.getTokenValueByLoginId(10001);
             return Result.ok(loginUser).put("token", token);
         }
         return Result.error("登录失败");
     }*/

    @MessageMapping(TOPIC)
    public void notifyLogin(Object object) {
        System.out.println("===我登录了====");
    }

    /**
     * ws发送消息测试
     */
    @GetMapping("/sendTest")
    public void send() {
        simpMessagingEnhanceTemplate.convertAndSendToUser("10001", TOPIC, "PC登录了");
    }
}
